package com.hengda.smart.speak.d.ac;

import android.content.Intent;
import android.graphics.drawable.ClipDrawable;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hengda.smart.speak.d.App;
import com.hengda.smart.speak.d.R;
import com.hengda.smart.speak.d.config.AppConfig;
import com.hengda.smart.speak.d.config.DbConfig;
import com.hengda.smart.speak.d.stc.ControlService;
import com.hengda.smart.speak.d.stc.StcLauncher;
import com.hengda.smart.speak.d.util.CommonUtil;
import com.hengda.smart.speak.d.util.SharePreferenceUtil;

public class SplashActivity extends AppCompatActivity implements Runnable{
    private RelativeLayout mViewContent;
    private TextView mTvKzVersion;
    private LinearLayout mLl;
    private ImageView mLogoImg;
    private ImageView mClipImg;
    private Button mBtnStart;
    private TextView mTvStartInfo;
    private TextView mBottomCompany;


    private ClipDrawable clipProgress;
    //+5S启动时间确保通信正常
    public final static int UP_DATA_GRESS = 1;
    public final static int GO_INTO_MAIN = 2;
    public final static int SHOW_PRO = 3;
    protected static final int LOAD_SDCARD = 5;
    protected static final int SHOW_BTN = 8;
    private int progressValue = 0;
    private int maxProgressValue = 10000;
    private int progressValueLoad = 0;
    private int maxProgressValueLoad = 10000;


    //工具类
    private StcLauncher lancherUtil;//STC下发工具
    private SharePreferenceUtil spUtil;    //当前配置文件信息
    private int a;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case UP_DATA_GRESS:
                    if (progressValue < maxProgressValue) {
                        progressValue = progressValue + 2000;
                        clipProgress.setLevel(progressValue);
                        if (progressValue == maxProgressValue) {
                            jumpTo();
                        } else {
                            handler.sendEmptyMessageDelayed(UP_DATA_GRESS, 100);
                        }
                    } else {
                        if (CommonUtil.checkFileExits(DbConfig.DB_PATH)) {
                            jumpTo();
                        }
                    }
                    break;
                case SHOW_PRO:
                    if (progressValueLoad < maxProgressValueLoad) {
                        progressValueLoad = progressValueLoad + 2000;
                        clipProgress.setLevel(progressValueLoad);
                        handler.sendEmptyMessageDelayed(SHOW_PRO, 1000);
                    } else {
                        jumpTo();
                    }
                    break;
                case SHOW_BTN:
                    mBtnStart.setVisibility(View.VISIBLE);
                    break;
                case GO_INTO_MAIN:
                    jumpTo();
                    break;

                default:
                    break;
            }
        }
    };

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case LOAD_SDCARD:
                    a++;
                    if (a >= 20) {
                        a = 0;
                        mHandler.removeMessages(LOAD_SDCARD);
                    }
                    if (isExternalStorageAvailable()) {
                        mHandler.removeMessages(LOAD_SDCARD);
                        if (!CommonUtil.checkFileExits(DbConfig.DB_PATH)) {
                            handler.sendEmptyMessage(SHOW_PRO);
                            new Thread(SplashActivity.this).start();
                        } else {
                            mBtnStart.setVisibility(View.VISIBLE);
                        }
                        mTvStartInfo.setVisibility(View.GONE);
                    } else {
                        mTvStartInfo.setText("正在加载，请稍等..." + a);
                        mHandler.sendEmptyMessageDelayed(LOAD_SDCARD, 1000);
                    }
                    break;
            }

        }
    };

    private boolean isExternalStorageAvailable() {
        return Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
                WindowManager.LayoutParams. FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);
        initViews();
        initData();
        initListener();
        initWidget();
    }

    public void initData() {
//		BWRUtil.checkBleState(this);
//		开启STC服务
        startService(new Intent(SplashActivity.this,ControlService.class));
        mHandler.sendEmptyMessage(LOAD_SDCARD);
        if(!isExternalStorageAvailable()){
            mBtnStart.setVisibility(View.GONE);
            mTvStartInfo.setVisibility(View.VISIBLE);
        }
    }

    private void initWidget() {
        initUtil();
        //todo
//            mTvKzVersion.setText(SystemTool.getAppVersionName(this));
        clipProgress = (ClipDrawable) mClipImg.getDrawable();
    }


    private void initUtil() {
        lancherUtil=new StcLauncher((App) getApplication(),this);
        spUtil = new SharePreferenceUtil(getApplicationContext(), AppConfig.SHARE_NAME);
        openAudio();
    }

    private void initListener() {
        mBtnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (progressValue == maxProgressValue || progressValue == 0) {
                    openAudio();
                    progressValue = 0;
                    handler.sendEmptyMessage(UP_DATA_GRESS);
                }
            }
        });
    }

    @Override
    public void run() {
        CommonUtil.LoadFile(getApplicationContext(), DbConfig.BASE_PATH
                        + "tem/", DbConfig.BASE_PATH + "tem/res_config.zip",
                R.raw.res_config);
        CommonUtil.unZip(DbConfig.BASE_PATH + "tem/res_config.zip",
                DbConfig.BASE_PATH);
        handler.sendEmptyMessageDelayed(SHOW_BTN, 500);
    }



    private void initViews() {
        mViewContent = (RelativeLayout) findViewById(R.id.view_content);
        mTvKzVersion = (TextView) findViewById(R.id.tv_kz_version);
        mLl = (LinearLayout) findViewById(R.id.ll);
        mLogoImg = (ImageView) findViewById(R.id.logo_img);
        mClipImg = (ImageView) findViewById(R.id.clip_img);
        mBtnStart = (Button) findViewById(R.id.btn_start);
        mTvStartInfo = (TextView) findViewById(R.id.tv_start_info);
        mBottomCompany = (TextView) findViewById(R.id.bottom_company);
    }

    /*打开音频模块*/
    private void openAudio() {
        lancherUtil.openAudio();
    }

    private void jumpTo() {
        startActivity(new Intent(SplashActivity.this,MainActivity.class));
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && keyCode == KeyEvent.KEYCODE_HOME) {
        }
        return false;
    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }
}
